<?php
use think\facade\Route;

// 全局CORS中间件
Route::group(function () {
    // 需要认证的路由组
    Route::group(function () {
        // 首页 - 需要认证
        Route::get('/', 'Index/index');
        
        // 测试路由 - 需要认证
        Route::get('/test', function() {
            return json([
                'code' => 200,
                'message' => 'Test路由工作正常',
                'data' => [
                    'timestamp' => time(),
                    'php_version' => PHP_VERSION
                ]
            ]);
        });
        
        // 更多测试路由 - 需要认证
        Route::get('/ping', function() {
            return 'pong';
        });
        
        Route::get('/info', function() {
            return json([
                'code' => 200,
                'message' => '系统信息',
                'data' => [
                    'php_version' => PHP_VERSION,
                    'thinkphp_version' => \think\App::VERSION,
                    'server_time' => date('Y-m-d H:i:s')
                ]
            ]);
        });
            })->middleware('JwtAuth');

    // API路由组
    Route::group('api', function () {
        // 认证相关路由 - 不需要认证
        Route::post('auth/login', 'Auth.Login/login');
        Route::post('auth/logout', 'Auth.Login/logout');
        Route::post('auth/refresh', 'Auth.Login/refresh');
        
        // 需要认证的路由组
        Route::group(function () {
            // 用户信息 - 需要认证
            Route::get('auth/profile', 'Auth.Login/profile');
            // 订单同步相关 - 更具体的路由在前面
            Route::post('orders/sync', 'Order.OrderSyncController/sync');
            Route::get('orders/sync/status', 'Order.OrderSyncController/status');
            Route::get('orders/sync/test', 'Order.OrderSyncController/testConnection');
            Route::get('orders/sync/records', 'Order.OrderSyncController/getSyncRecords');
            Route::get('orders/sync/records/:id', 'Order.OrderSyncController/getSyncRecordDetail');
            Route::get('orders/sync/running-tasks', 'Order.OrderSyncController/getRunningTasks');
            
            // 订单相关
            Route::get('orders', 'Order.OrderController/getOrderList');
            Route::get('orders/:id', 'Order.OrderController/getOrderDetail');
            Route::get('orders/stats', 'Order.OrderController/getOrderStats');
            Route::post('orders/import', 'Order.Order/import');
            Route::put('orders/:id', 'Order.Order/update');
            Route::delete('orders/:id', 'Order.Order/delete');
            Route::get('orders/export', 'Order.Order/export');
            Route::get('orders/statistics', 'Order.Order/statistics');
            
            // 数据导入相关
            Route::post('import/upload', 'Import.Import/upload');
            Route::post('import/validate', 'Import.Import/validate');
            Route::post('import/process', 'Import.Import/process');
            Route::get('import/logs', 'Import.Import/logs');
            
            // 多维度数据导入
            Route::post('import/douyin-settlement', 'Import.Import/douyinSettlement');
            Route::post('import/douyin-pending', 'Import.Import/douyinPending');
            Route::post('import/douyin-insurance', 'Import.Import/douyinInsurance');
            Route::post('import/ali-purchase', 'Import.Import/aliPurchase');
            
            // 结算数据相关
            Route::get('settlement/douyin', 'Settlement.Settlement/douyin');
            Route::get('settlement/douyin/:id', 'Settlement.Settlement/douyinDetail');
            Route::get('settlement/pending', 'Settlement.Settlement/pending');
            Route::get('settlement/statistics', 'Settlement.Settlement/statistics');
            
            // 保险数据相关
            Route::get('insurance/douyin', 'Insurance.Insurance/douyin');
            Route::get('insurance/douyin/:id', 'Insurance.Insurance/douyinDetail');
            Route::get('insurance/statistics', 'Insurance.Insurance/statistics');
            
            // 采购数据相关
            Route::get('purchase/ali', 'Purchase.Purchase/ali');
            Route::get('purchase/ali/:id', 'Purchase.Purchase/aliDetail');
            Route::get('purchase/statistics', 'Purchase.Purchase/statistics');
            
            // 报表相关
            Route::get('reports/profit', 'Report.Report/profit');
            Route::get('reports/order', 'Report.Report/order');
            Route::get('reports/platform', 'Report.Report/platform');
            Route::get('reports/cost', 'Report.Report/cost');
            
            // 系统配置相关 - 更具体的路由在前面
            Route::get('system-configs/external-api', 'System.SystemConfigController/getExternalApiConfig');
            Route::put('system-configs/external-api', 'System.SystemConfigController/updateExternalApiConfig');
            Route::get('system-configs/test-token', 'System.SystemConfigController/testToken');
            Route::get('system-configs', 'System.SystemConfigController/index');
            Route::get('system-configs/:id', 'System.SystemConfigController/show');
            Route::put('system-configs', 'System.SystemConfigController/update');
            
            // 操作日志相关
            Route::get('logs', 'System.LogController/index');
            Route::get('logs/:id', 'System.LogController/show');
            Route::get('logs/statistics', 'System.LogController/statistics');
            Route::post('logs/clean', 'System.LogController/clean');
            Route::get('logs/export', 'System.LogController/export');
            Route::get('logs/modules', 'System.LogController/modules');
            Route::get('logs/statuses', 'System.LogController/statuses');
            
            // 测试路由
            Route::get('test-config', 'TestConfig/index');
            
        })->middleware('JwtAuth');
        
    });
    
})->middleware('Cors'); 